package com.example.sqlitedemo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class DataPreparation {
	
	public static void DataProcessing(File file, File outputPath) {
		String outputFileName = outputPath.getPath() + "\\" + file.getName();
		//Reader reader = new FileInputStream(File)
		BufferedReader in;
		BufferedWriter out;
		boolean isTitled = true;
		
		try{
			in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFileName), "UTF-8"));
			
			String stringLine;
			
			/* Write the start section */
			String start = "<html>" +
							"<body align=\"justify\">" +
							"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">" +
							"<b><font color=\"red\">";
			
			out.write(start);
			out.newLine();
			
			/* Write the content section */
			while((stringLine = in.readLine()) != null) {
				if(stringLine.equals("")) {
					if(isTitled == true) {
						stringLine = "</font></b>";
						isTitled = false;
					}
				} else if(stringLine.equals("Lá»�i BÃ n") || stringLine.equals("Lá»�i TrÃ­ch")) {
					stringLine = "<b><font color=\"blue\">" + stringLine +"</font></b>";
				}
				
				
				out.write(stringLine + "<br>");
				out.newLine();
			}
			
			/* Write the end section */
			String end ="</body></html>";
			out.write(end);
			
			in.close();
			out.close();
			
		} catch (Exception e) {
			System.out.println(e);
		}
		
	}

	private static void PureDataProcessing(File file, File outputPath) {
		String outputFileName = outputPath.getPath() + "\\" + file.getName();
		//int index = file.getName().indexOf('.');
		String fileTmp = outputPath.getPath() + "\\" + file.getName().replace(".", "_2.");
		//System.out.println(fileTmp);
		
		//Reader reader = new FileInputStream(File)
		BufferedReader in;
		BufferedWriter out;
		BufferedWriter out2;
		boolean isWritingPart = false;
		int isDouble = 0;
		
		try{
			in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFileName), "UTF-8"));
			
			
			String stringLine;
			
			/* Write the content section */
			while((stringLine = in.readLine()) != null) {
				if(stringLine.equals("<b><font color=\"red\">")) {
					//System.out.println("Start writing ...");
					isDouble++;
					isWritingPart = true;
					if(isDouble == 2) {
						System.out.println(file.getPath() + " : is Double");
						out2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileTmp), "UTF-8"));
						while((stringLine = in.readLine()) != null) {
							if(stringLine.equals("</font>") || stringLine.equals("</b>")) {
								continue;
							}
							
							if(stringLine.equals("</body>")) {
								break;
							}
							
							String tmp = StringProcessing(stringLine);
							out2.write(tmp);
							out2.newLine(); 
						}
						out2.close();
						break;
					}
					continue;
				}
				
				if(stringLine.equals("</body>")) {
					//System.out.println("Stop writing ...");
					isWritingPart = false;
				}
				
				if(isWritingPart == true) {
					//System.out.println("is writing");
					
					if(stringLine.equals("</b>") || stringLine.equals("</font>")) {
						continue;
					}
					
					String tmp = StringProcessing(stringLine);
					out.write(tmp);
					out.newLine();
					
					
				}
			}
			
			//System.out.println("Stop writing ...");
			in.close();
			out.close();
			
		} catch (Exception e) {
			System.out.println(e);
		}
		
	}
	
	private static String StringProcessing(String str) {
		String tmp = str.replaceAll("<br>", "");
		
		tmp = tmp.replace("\\\"", "\"");
		tmp = tmp.replace("\\\'", "\'");
		tmp = tmp.replaceAll("<b><font color=\"blue\">Lá»�i BÃ n</font></b>", "Lá»�i BÃ n");
		tmp = tmp.replaceAll("<b><font color=\"blue\">Lá»�i TrÃ­ch</font></b>", "Lá»�i TrÃ­ch");
		//tmp = tmp.replace("</font>", "");
		//tmp = tmp.replace("</b>", "");
		
		return tmp;
	}
	
	/**
	 * @param args
	 */

//	public static void main(String[] args) {
//		/* Check input parameter */
//		if(args.length != 1) {
//			System.out.println("Error, lack of input path");
//			System.exit(1);
//		}
//		
//		/* Get input path */
//		System.out.println("Input path: " + args[0]);
//		File inputPath = new File(args[0]);
//		
//		if(!inputPath.isDirectory()) {
//			System.out.println("Input path isn't a directory");
//			System.exit(1);
//		}
//		
//		/* Create new Output folder */
//		File outputPath = new File(inputPath.getParent() + "\\Output");
//		outputPath.delete();
//		outputPath.mkdir();
//
//		/* Get all files in input path */
//		File listOfFiles[] = inputPath.listFiles();
//		if(listOfFiles.length == 0) {
//			System.out.println("Input path is empty folder");
//			System.exit(1);
//		}
//		
//		/* Process for single file */
//		for(int i = 0; i < listOfFiles.length; i++) {
//			//System.out.println(listOfFiles[i].getPath());
//			DataProcessing(listOfFiles[i], outputPath);
//			//PureDataProcessing(listOfFiles[i], outputPath);
//		}
//
//	}


}
